package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.api.types.VoidFunction
import io.shuttle.mbe.core.Promise

////////////////////
// Instance ID
////////////////////
/**
 * Use `Browser.instanceID` to access the Instance ID service.
 *
 * Permissions: "gcm"
 * @since Chrome 44
 */
@ChromeMinVersion(44)
interface InstanceID {
    // Resets the app instance identifier and revokes all tokens associated with it.
    @PromiseStyleMinVersion(96)
    fun deleteID(
        callback: VoidFunction? = null
    ): Promise<Void>

    // Revokes a granted token.
    @PromiseStyleMinVersion(96)
    fun deleteToken(
        deleteTokenParam: DeleteTokenParam,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Retrieves the time when the InstanceID has been generated.
    // The creation time will be returned by the callback.
    // @return {callback.creationTime} The time when the Instance ID has been generated,
    // represented in milliseconds since the epoch.
    @PromiseStyleMinVersion(96)
    fun getCreationTime(
        callback: Value1Function<Number>? = null
    ): Promise<Number>

    // Retrieves an identifier for the app instance.
    // The instance ID will be returned by the callback.
    // The same ID will be returned as long as the application identity has not been revoked or expired.
    // @return {callback.instanceID} An Instance ID assigned to the app instance.
    @PromiseStyleMinVersion(96)
    fun getID(
        callback: Value1Function<String>? = null
    ): Promise<String>

    // Return a token that allows the authorized entity to access the service defined by scope.
    // @return {callback.token} A token assigned by the requested service.
    @PromiseStyleMinVersion(96)
    fun getToken(
        getTokenParam: GetTokenParam,
        callback: Value1Function<String>? = null
    ): Promise<String>

    // Fired when all the granted tokens need to be refreshed.
    val onTokenRefresh: Events.Event<VoidFunction>

    data class DeleteTokenParam(
        // The authorized entity that is used to obtain the token.
        @ChromeMinVersion(46)
        var authorizedEntity: String,
        // The scope that is used to obtain the token.
        @ChromeMinVersion(46)
        var scope: String,
    )

    data class GetTokenParam(
        // The authorized entity that is used to obtain the token.
        @ChromeMinVersion(46)
        var authorizedEntity: String,
        // The scope that is used to obtain the token.
        @ChromeMinVersion(46)
        var scope: String,
        // Allows including a small number of string key/value pairs that will be associated with the token and may be used in processing the request.
        @Deprecated(
            "Deprecated since Chrome 89",
            replaceWith = ReplaceWith("options are deprecated and will be ignored.")
        )
        @ChromeMinVersion(46)
        var options: Any?,
    )
}